package cn.zhaoyuening.hnistalumnirecord.web.controller;

import cn.zhaoyuening.hnistalumnirecord.domain.AdminUser;
import cn.zhaoyuening.hnistalumnirecord.domain.ConfigItem;
import cn.zhaoyuening.hnistalumnirecord.domain.News;
import cn.zhaoyuening.hnistalumnirecord.domain.NewsModule;
import cn.zhaoyuening.hnistalumnirecord.service.AdminUserService;
import cn.zhaoyuening.hnistalumnirecord.service.NewsModuleService;
import cn.zhaoyuening.hnistalumnirecord.service.NewsService;
import com.mysql.jdbc.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpSession;
import java.util.List;

/**
 * Created by zhao on 17-1-17.
 */
@Controller
@RequestMapping("/hello")
//处理首页、后台登录
public class HelloController extends BaseController{

    @RequestMapping("/index.do")
    //跳转到首页
    public String index(Model model){
        //返回模块id 1 2 3 名称
        NewsModule module1 = getModuleService().findById(1);
        NewsModule module2 = getModuleService().findById(2);
        NewsModule module3 = getModuleService().findById(3);
        ConfigItem configItem = getConfigItemService().findById(1);
        if (module1 != null) {
            model.addAttribute("module1Name", module1.getName());
        }
        if (module2 != null) {
            model.addAttribute("module2Name", module2.getName());
        }
        if (module3 != null) {
            model.addAttribute("module3Name", module3.getName());
        }


        return "index";
    }

    @RequestMapping("/loginAdminUi.do")
    //直接跳转到登录后台界面
    public String loginAdminUi(){
        return "adminLogin";
    }


    @RequestMapping("/loginAdmin.do")
    //登录后台接口，成功保存AdminUser到会话当中
    public String loginAdmin(@ModelAttribute  AdminUser adminUser, Model model, HttpSession session) {
        AdminUser user = getAdminUserService().isLoginSuccess(adminUser);
        if (user == null) {
            //登录失败
            return index(model);
        }
        //登录成功
        session.setAttribute("adminLoginUser",user);
        return "redirect:/admin/index.do";
    }




}
